package cn.zh.controller;

import cn.zh.commons.ResponseResult;
import cn.zh.entity.Admin;
import cn.zh.service.AdminService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("admin")
public class AdminController {
    @Resource
    private AdminService adminService;

    //管理员登录
    @RequestMapping("front/login")
    public ResponseResult login(String username1,String password1,String captcha, HttpServletRequest request){
        return adminService.login(username1,password1,captcha,request);
    }

    // 获取管理员个人信息
    @RequestMapping("personalInfo")
    public ResponseResult personalInfo(String username){

        return adminService.getPersonalInfo(username);
    }

    // 个人信息修改
    @RequestMapping("personal/edit")
    public ResponseResult  editPersonalInfo(Admin admin){

        return adminService.editPersonalInfo(admin);
    }
    // 个人密码修改
    @RequestMapping("personal/password")
    public ResponseResult  editPersonalPassword(String oldPassword, String password, HttpServletRequest request){

        return adminService.editPersonalPassword(oldPassword,password,request);
    }
    // 退出登录
    @RequestMapping("logout")
    public ResponseResult  logout(HttpServletRequest request){

        HttpSession session = request.getSession();
        // 清空session
        session.invalidate();
        return ResponseResult.success().message("安全退出");
    }

    @RequestMapping("page")
    public ResponseResult adminPage(Integer page, Integer limit, String username) {
        return this.adminService.adminPage(page, limit, username);
    }

    @RequestMapping("add")
    public ResponseResult addAdmin(Admin admin) {
        return this.adminService.addAdmin(admin);
    }

    @RequestMapping("delete")
    public ResponseResult deleteAdmin(@RequestBody List<Integer> ids) {
        return this.adminService.removeAdmin(ids);
    }

    @RequestMapping("resetPassword")
    public ResponseResult resetPassword(Integer id) {
        return this.adminService.resetPassword(id);
    }

    @RequestMapping("getById")
    public ResponseResult getByAdminId(Integer id) {
        return this.adminService.getByAdminId(id);
    }

    @RequestMapping("edit")
    public ResponseResult editAdmin(Admin admin) {
        return this.adminService.editAdmin(admin);
    }



}
